import $ from '../common';
Page({
  data: {
    titleHeight: $.gData().titleHeight,
    statuHeight: $.gData().statuHeight,
    info: {},
    imgList: [],
    address: ''
  },
  getinfo() {
    const {lesson_uuid} = this.data
    $.get('/teacher/sign/detail', {
      lesson_uuid
    }, res => {
      this.setData({
        info: res.lesson
      })
    })
  },
  afterRead(event) { //上传图片
    const {
      file
    } = event.detail;
    const {
      info
    } = this.data
    let files = file.map(v => { //因为vant 返回的解构是[{url:'............'}] $.uploadFileList 需要的结构式[1,2,3]
      return v.url
    })
    $.uploadFileList("/image/upload/oss", {
      prefix: "keday/",
      fileList: files
    }, res => {
      let a = res.map(i => { //返回的结构式[1,2,3,5] 需要改成vant需要的[{url:'............'},{}]
        return {
          url: i
        }
      })
      this.setData({
        'imgList': a
      })
    })
  },
  imgdelete(event) { //删除图片
    const {
      imgList
    } = this.data
    imgList.splice(event.detail.index, 1)
    this.setData({
      imgList
    })
  },
  // 签到
  signhand() {
    const {address,imgList,lesson_uuid,lnglat} = this.data
    if (address == '') {
      wx.showToast({
        title: '请先获取您当前的位置',
        icon:'error'
      })
      return
    }
    if (imgList.length == 0) {
      // $.msg('请先拍摄签到地的照片')
      wx.showToast({
        title: '请先拍摄签到地的照片',
        icon:'error'
      })
      return
    }
    let imgarr=[]
    imgList.map((v,i)=>{
      imgarr[i]=v.url
    })
    let obj={
      zonename:address,
      imgs:imgarr,
      lnglat,
      lesson_uuid
    }
    $.navigateTo(`/Signin/waitsign?obj=${JSON.stringify(obj)}`)
    return
    $.post('/teacher/sign/add',{
      zonename:address,
      imgs:imgarr,
      lnglat,
      lesson_uuid
    },res=>{
      wx.showToast({
        title: '签到成功',
        icon: 'success',
        duration: 2000,
        mask:ture,
        success:res=>{
          setTimeout(() => {
              wx.navigateBack({
                delta: -1,
              })            
          }, 2000);
        }
      })
    },fail=>{
      if(fail.data.message='不在签到范围内, 请联系巡课人员或重新上传!'){
        wx.showModal({
          title:'提示',
          content:fail.data.message
        })
      }
    })
  },
  // 刷新
  refresh(){
    this.check((res=>{
      this.getaddress()
    }))
  },
 // 检查权限
  check(ck){
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userLocation'] != true) {
          console.log('没有授权');
          wx.authorize({ //授权弹框
            scope: 'scope.userLocation',
            success: () => {
              console.log('授权弹框授权成功');
              ck&&ck()
            },
            fail(err) { //拒绝后提示内容
              wx.showModal({
                title: '提示',
                content: '请开启定位授权后重试',
                success(res) {
                  if (res.confirm) {
                    wx.openSetting({
                      success(res) {
                        console.log(res.authSetting);
                        ck&&ck()
                      }
                    })
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })
            }
          })
        } else {
          console.log('已授权');
          ck&&ck()
        }
      }
    })
  },
  // 获取地址位置信息
  getaddress() {
    this.setData({
      address: ''
    })
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        const latitude = res.latitude//纬度
        const longitude = res.longitude//经度
        wx.showLoading({
          title: '定位中',
        })
        this.setData({
          lnglat:longitude+','+latitude
        })
        wx.request({
          url: 'https://restapi.amap.com/v3/geocode/regeo',
          data: {
            key: '6313d1bbd7f3ac72a5a13825a131f771',
            location: longitude + "," + latitude,
            extensions: "all",
            s: "rsx",
            sdkversion: "sdkversion",
            logversion: "logversion"
          },
          success: (res) => {
            let address = res.data.regeocode.formatted_address.split('市')
            this.setData({
              address: address[1]
            })
          },
          fail: fail => {
            this.setData({
              address: '定位失败'
            })
          },
          complete:(all)=>{
            wx.hideLoading()
          }
        })
      }
    })
  },
  onLoad(e) {
    const {lesson_uuid,sign_status=''} = e
    this.setData({ lesson_uuid,sign_status})
    this.getinfo()
    if(sign_status=='daynot' ||sign_status==''){
      this.check((res=>{
        this.getaddress()
      }))
    }
   
  },
  onShow() {
  },
})